package com.qingxin.park.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.entity.SysUserParkRel;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.SysUser;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.StringUtils;

/**
 * 用户-园区
 * @author Administrator
 *
 */
@Service
public class SysUserParkRelService extends BaseService<SysUserParkRel, String> {
	
	@Autowired
	private FarmParkService farmParkService;

	
	/**
	 * 添加用户园区关系
	 * @param userid
	 * @param parkid
	 * @param loginUser
	 * @return
	 */
	public SysError addUserParkRel(String userid, String parkid, LoginUser loginUser){
		if(StringUtils.isEmpty(userid) || StringUtils.isEmpty(parkid)){
			return null;
		}
		Object id = getByHql("select id from SysUserParkRel where sysUser.userid=? and farmPark.parkid=?", userid, parkid);
		if(id != null){
			return null;
		}
		SysUserParkRel rel = new SysUserParkRel(new FarmPark(parkid), new SysUser(userid), loginUser.getUsername(), new Date());
		this.save(rel);
		return null;
	}
	
	
	/**
	 * 设置用户的园区查看权限
	 * @param userid
	 * @param parkids
	 * @param loginUser
	 * @return
	 */
	public SysError saveUserParkRels(String userid, String[] parkids, LoginUser loginUser){
		if(StringUtils.isEmpty(userid)){
			return null;
		}
		//删除现有关联
		deleteByUserid(userid);
		
		if(parkids==null || parkids.length==0){
			return null;
		}
		
		Date now = new Date();
		String createBy = loginUser.getLogin_name();
		
		for(String parkid : parkids){
			SysUserParkRel rel = new SysUserParkRel(new FarmPark(parkid), new SysUser(userid), createBy, now);
			this.save(rel);
		}
		
		return null;
	}
	
	
	/**
	 * 获取指定用户关联的园区id集合
	 * @param userid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Set<String> getUserParkids(String userid){
		Set<String> set = new HashSet<String>();
		if(StringUtils.isEmpty(userid) ){			
			return set;
		}
		
		List<String> list = listByHql("select farmPark.parkid from SysUserParkRel where sysUser.userid=?", userid);
		if(list != null){
			set.addAll(list);
		}
		return set;
	}
	
	
	/**
	 * 删除用户园区关联关系
	 * @param userid
	 * @return
	 */
	public SysError deleteByUserid(String userid){
		deleteByHql("delete from SysUserParkRel where sysUser.userid=?", userid);
		return null;
	}
	
	
	/**
	 * 获取指定园区关联的园区用户
	 * @param parkid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SysUser> getParkUserData(String parkid){
		FarmPark farmPark = farmParkService.getSafeEntity(parkid);
		
		List<SysUser> list = listByHql("select new com.qingxin.sys.entity.SysUser(u.userid, u.login_name, u.username)" +
				" from SysUser u where u.user_type='2'" +
				" and u.groupid like '"+farmPark.getGroupid()+"%'" +
				" and u.userid in (select distinct v.sysUser.userid from SysUserParkRel v where v.farmPark.parkid=?)" +
				" order by u.username asc", parkid);
		if(list == null){
			return new ArrayList<SysUser>();
		}
		return list;
	}
	
	
	
}
